#include <windows.h> 
#include <Commctrl.h>

#include "resource.h"
#include "ListViewCtrl.hpp"

namespace GUI
{
	const int C_COLUMNS = 9;
	const int COLUM_WIDTH = 100;

	const LPCWSTR WINDOW_NAME = L"NetworkList";

	ListViewCtrl::ListViewCtrl(HWND hWndParent, HINSTANCE hInst)
	{
		/*INITCOMMONCONTROLSEX InitCtrlEx;
		InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
		InitCtrlEx.dwICC  = ICC_LISTVIEW_CLASSES;
		InitCommonControlsEx(&InitCtrlEx);*/

		this->hInst = hInst;

		RECT rcClient;
		GetClientRect(hWndParent, &rcClient);
		int width = rcClient.right - rcClient.left;
		int height = rcClient.bottom - rcClient.top;

		/*hListView =  CreateWindowEx (WS_EX_OVERLAPPEDWINDOW,
			WC_LISTVIEW, WINDOW_NAME,
			WS_CHILD | WS_VISIBLE  | LVS_REPORT,
			CW_USEDEFAULT, CW_USEDEFAULT,
			(rcClient.right - rcClient.left), (rcClient.bottom - rcClient.top),
			hwndMain, NULL,	hInst, NULL);*/ 


		//ListView_SetBkColor(hListView, COLOR_WINDOW);


		hListView = GetDlgItem(hWndParent, IDC_NETWORK_LIST);

		Init();
	}


	BOOL ListViewCtrl::Init() 
	{ 
		WCHAR szText[256]; 
		LVCOLUMN lvc;


		// Initialize the LVCOLUMN structure.
		// The mask specifies that the format, width, text,
		// and subitem members of the structure are valid.
		lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

		// Add the columns.
		for (int iCol = 1; iCol < C_COLUMNS; iCol++)
		{
			lvc.iSubItem = iCol;
			lvc.pszText = szText;
			lvc.cx = COLUM_WIDTH;               // Width of column in pixels.

			if ( iCol < 2 )
				lvc.fmt = LVCFMT_LEFT;  // Left-aligned column.
			else
				lvc.fmt = LVCFMT_RIGHT; // Right-aligned column.

			// Load the names of the column headings from the string resources.
			LoadString(hInst, IDS_FIRSTCOLUMN + iCol, szText, sizeof(szText)/sizeof(szText[0]));

			// Insert the columns into the list view.
			if (ListView_InsertColumn(hListView, iCol, &lvc) == -1)
				return FALSE;
		}

		return TRUE;
	} 

	void ListViewCtrl::Clear()
	{
		ListView_DeleteAllItems(hListView);
	}

	void ListViewCtrl::InsertItems(Network::NetworkList& networkList)
	{

		Network::NetworkList::iterator itr = networkList.begin();

		for(; itr != networkList.end(); ++itr)
		{
			InsertItem(*itr);
		}		
	}

	void ListViewCtrl::InsertItem(Network::Network_& network)
	{
		LVITEM LvItem;  
		memset(&LvItem,0,sizeof(LvItem)); 
		LvItem.mask  = LVIF_TEXT;

		//******** 1 **************
		LvItem.iItem = 0;
		LvItem.iSubItem = IDS_NETWORK_NAME - IDS_FIRSTCOLUMN - 1;
		LvItem.pszText = network.name;
		ListView_InsertItem(hListView, &LvItem);

		//******** 2 **************
		LvItem.pszText = network.connectivity;
		LvItem.iSubItem = IDS_STATE - IDS_FIRSTCOLUMN - 1;
		ListView_SetItem(hListView, &LvItem);

		//******** 3 **************
		LvItem.pszText = network.networkId;
		LvItem.iSubItem = IDS_NETWORKID - IDS_FIRSTCOLUMN - 1;
		ListView_SetItem(hListView, &LvItem);


		//******** 4 **************
		LvItem.pszText = network.domainType;
		LvItem.iSubItem = IDS_DOMAINTYPE - IDS_FIRSTCOLUMN - 1;
		ListView_SetItem(hListView, &LvItem);


		//******** 5 **************
		LvItem.pszText = network.description;
		LvItem.iSubItem = IDS_DESCRIPTION - IDS_FIRSTCOLUMN - 1;
		ListView_SetItem(hListView, &LvItem);

		//******** 6 **************
		
		LvItem.pszText = network.category;
		LvItem.iSubItem = IDS_CATEGORY - IDS_FIRSTCOLUMN - 1;
		ListView_SetItem(hListView, &LvItem);

		//******** 7 **************

		LvItem.pszText = network.timeCreated;
		LvItem.iSubItem = IDS_TIME_CREATED - IDS_FIRSTCOLUMN - 1;
		ListView_SetItem(hListView, &LvItem);

		//******** 8 **************

		LvItem.pszText = network.timeConnected;
		LvItem.iSubItem = IDS_TIME_CONNECTED - IDS_FIRSTCOLUMN - 1;
		ListView_SetItem(hListView, &LvItem);

		//ListView_SetBkColor(hListView, RGB(45,0,0));

	}
}